<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="专业名称" prop="majorName">
        <el-input
          v-model="queryParams.majorName"
          placeholder="专业名称"
          clearable
          @keyup.enter.native="searchButton"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="searchButton"
          >搜索</el-button
        >
        <el-button type="primary" size="mini" @click="handleQuery"
          >批量删除</el-button
        >
        <el-button type="primary" size="mini" @click="handleQuery"
          >添加专业</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="display"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="专业名称" align="center" prop="majorName" />
      <el-table-column
        label="专业名称缩写"
        align="center"
        prop="majorAbbreviation"
      />
      <el-table-column
        label="专业描述"
        align="center"
        prop="majorDescription"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { searching, listPost } from '@/api/profession/post'

export default {
  name: 'Post',
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 显示表格数据
      display: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 专业参数
      queryParams: {
        subMajorName: '',
        pageNum: 1,
        pageSize: 10,
        majorName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        majorName: [
          { required: true, message: '专业名称不能为空', trigger: 'blur' }
        ],
        majorAbbreviation: [
          { required: true, message: '专业名称缩写不能为空', trigger: 'blur' }
        ],
        majorDescription: [
          { required: true, message: '专业描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  // 表单重置
  reset () {
    this.form = {
      majorName: undefined,
      majorAbbreviation: undefined,
      majorDescription: undefined
    }
    this.resetForm('form')
  },
  // 取消按钮
  cancel () {
    this.open = false
    this.reset()
  },
  methods: {
    /** 查询岗位列表 */
    getList () {
      this.loading = true
      listPost(this.queryParams).then(response => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索专业列表 */
    searchingMajor () {
      this.loading = true
      searching(this.queryParams).then(response => {
        this.display = response
        console.log('11')
        console.log(this.display)
        this.loading = false
      })
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      // 用组件的 reset 方法，重置表单中的数据。这通常包括清空表单中的字段值，重置验证状态等
      this.reset()
      //       row.postId：获取传入参数 row 中的 postId。row 是当前行的数据对象，通常包含该行的所有字段值。
      //       this.ids：当 row.postId 不存在时，使用 this.ids。这通常是一个数组，包含选中记录的ID。
      const postId = row.id || this.ids
      //       getPost(postId)：调用 getPost 函数，传递 postId 以获取岗位数据。假设 getPost 是一个返回Promise的API请求函数
      searching(postId).then(response => {
        // this.form = response.data：将API响应中的数据赋值给 this.form，以便在表单中显示。
        // this.open = true：设置 open 为 true，显示对话框。
        // this.title = '修改岗位'：设置对话框的标题为“修改岗位”。
        this.form = response
        this.open = true
        this.title = '修改岗位'
      })
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '添加岗位'
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.postId != undefined) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 搜索按钮操作 */
    searchButton () {
      this.searchingMajor()
    }
  }
}
</script>
